<?php

namespace api\common\models;
use yii\filters\RateLimitInterface;
use yii\db\ActiveRecord;
use api\common\models\Rate;
use yii\web\HttpException;
use yii\web\Link;
use yii\web\ServerErrorHttpException;
use yii\web\Request;
use yii\web\NotFoundHttpException;
use api\cs\ftps;
/*
 * 速率限制
 * Class Common
 * @package api\common\models
 */
class Common extends ActiveRecord
{
    public $linksEnvelope = '_links';
    public $metaEnvelope = '_meta';

    /*
     * 获取图片的基础路径
     */
    public static function getImgBaseUrl(){
        if(\Yii::$app->params['img_base_url'] == ''){
            return \Yii::$app->request->hostInfo;
        }
        return \Yii::$app->params['img_base_url'];
    }

    /*
     * 获取html的基础路径
     */
    public static function getHtmlBaseUrl(){
        if(\Yii::$app->params['img_html_url'] == ''){
            return \Yii::$app->request->hostInfo;
        }
        return \Yii::$app->params['img_html_url'];
    }

    public function serializePagination($pagination)
    {
        $this->linksEnvelope = Link::serialize($pagination->getLinks(true));
        $this->metaEnvelope = [
            'totalCount' => $pagination->totalCount,
            'pageCount' => $pagination->getPageCount(),
            'currentPage' => $pagination->getPage() + 1,
            'perPage' => $pagination->getPageSize(),
        ];
    }


    /*
     * 分表使用动态计算表名称 根据id区间计算
     * @param $id
     * @param int $num
     * @param $tablename
     */
    public static function formatTable($id,$num=10000,$tablename){
        if($id==''){
            throw new ServerErrorHttpException('由于不明原因未能计算数据表.');
        }
        $suffix = \Yii::$app->db->tablePrefix.$tablename.'_'.(ceil($id/$num)*$num);
        return $suffix;
    }

    /*
     * 根据access-token获取用户id
     * @return string
     * @throws NotFoundHttpException
     */
    public function getTokenUserId(){
        $request = new Request();
        $accessToken = $request->get('access-token');
        if(empty($accessToken)) $accessToken = $request->getHeaders()->get('access-token');
        if (is_string($accessToken)) {
            $query = new \yii\db\Query();
            if($data = $query->from('{{%user_app}}')->select('id,username,collect_category,nickname,phone,score')->where(['access_token'=>$accessToken])->one()){
                return $data;
            }
            throw new NotFoundHttpException('access-token错误.');
        }
        return '';
    }


    /*
     * FTP上传文件
     * @param $remote 远程目录
     * @param $local 本地文件
     * @param int $unlink 是的删除本地文件
     * @return string
     * @throws ServerErrorHttpException
     */
    public static function ftp_upload($remote,$local,$unlink=0){
        if(\Yii::$app->params['ftp_start']){
            $ftps = new ftps();
            $host = \Yii::$app->params['ftp_host'];
            $username = \Yii::$app->params['ftp_username'];
            $password = \Yii::$app->params['ftp_password'];
            $post = \Yii::$app->params['ftp_port'];
            $pasv = \Yii::$app->params['ftp_pasv'];
            $ssl = \Yii::$app->params['ftp_ssl'];
            $timeout = \Yii::$app->params['ftp_timeout'];
            $base_dir = \Yii::$app->params['ftp_base_dir'];
            $url = \Yii::$app->params['ftp_url'];
            if(!$ftps->connect($host,$username,$password,$post,$pasv,$ssl,$timeout)){
                throw new ServerErrorHttpException($ftps->get_error());
            }

            $_remote = $base_dir.$remote;
            if(!$ftps->put($_remote,$local)){
                throw new ServerErrorHttpException($ftps->get_error());
            }

            if($unlink == 1){
                @unlink($local);
            }
            return $url.$remote;
        }
    }

    /*
     * 格式化提示语
     * @param $type
     * @return string
     */
    public static function formatType($type){
        if($type == 'qq'){
            return 'QQ';
        }elseif($type == 'wx'){
            return '微信';
        }elseif($type == 'wb'){
            return '微博';
        }else{
            return '未知第三方';
        }
    }
}
